﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;


    public class ColorModule : Singleton<ColorModule>
    {
        public Gradient GradientA;
        private Dictionary<Type, ColorProcess> _colorSchemeDatas = new Dictionary<Type, ColorProcess>();
        protected override void Awake()
        {
            base.Awake();
            var lists = GetDerivedTypes(typeof(ColorProcess));
            foreach (var progress in lists)
            {
                ColorNameAttritule colorNameAttritule = progress.GetCustomAttribute<ColorNameAttritule>();
                if (colorNameAttritule != null)
                {
                    _colorSchemeDatas.Add(colorNameAttritule.colorType,(ColorProcess)Activator.CreateInstance(progress));
                }
            }
        }

        public void Motify(ColorSchemeData data,int colorIndex)
        {
            
            if (_colorSchemeDatas.ContainsKey(data.GetType()))
            {
                _colorSchemeDatas[data.GetType()].Progress(data,colorIndex);
            }
        }
        
        
         private List<System.Type> GetDerivedTypes(System.Type baseType)
         {
             var derivedTypes = new List<System.Type>();
             var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();

             foreach (var assembly in assemblies)
             {
                 var types = assembly.GetTypes();
                 foreach (var type in types)
                 {
                     if (type.IsClass && !type.IsAbstract && baseType.IsAssignableFrom(type))
                     {
                         derivedTypes.Add(type);
                     }
                 }
             }

             return derivedTypes;
         }
    }
